function trackermachine()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=trackermachine&tmpl=raw";
	var callback = function(){
		if($("#grid_trackermachine").length > 0)
		{
			var dataSource = $("#grid_trackermachine").data("kendoGrid").dataSource;
			dataSource.read();
			
		}	
	}
	obj.callback = callback;
	return obj;
}

function trackermachine_addlinhkien(vendor)
{
	if($(".linhkien_select option:selected").length == 0)
	{
		alertify.alert("Vui lòng chọn linh kiện");
	}else
	{
		var option = $(".linhkien_select option:selected");
		var row = JSON.parse($(option).attr("data-json"));
		var check = false;
		$("#table_linhkien tbody tr").each(function(){
			if($(this).find(".idlinhkien_hidden").val() == row.id)
			{
				check = true;
				return;
			}	
		})
		if(!check)
		{
			var tr = $("<tr></tr>");
			var td1 = $("<td></td>");
			var td2 = $("<td></td>");
			var td3 = $("<td></td>");
			var td4 = $("<td></td>");
			var td5 = $("<td></td>");
			var td6 = $("<td class='chosen_nocreate'></td>");
			var td7 = $("<td></td>");
			tr.append(td1);
			tr.append(td2);
			tr.append(td3);
			tr.append(td4);
			tr.append(td5);
			tr.append(td6);
			tr.append(td7);
			td1.append("<input type='hidden' name='linhkien_id[]' class='idlinhkien_hidden' value='"+row.id+"' />" + row.ma);
			td2.append(row.ten);
			var thangbaohanh = $("<input type='text' name='sothangbaohanh_"+row.id+"' value='"+row.sothangbaohanh+"'/>");
			var controlcustomthang = $("<div class='controls custom'></div>");
			controlcustomthang.append(thangbaohanh);
			td3.append(controlcustomthang);
			var counterbaohanh = $("<input type='text' name='sodungluongbaohanh_"+row.id+"' value='"+row.sodungluongbaohanh+"'/>");
			var controlcustomcounter = $("<div class='controls custom'></div>");
			controlcustomcounter.append(counterbaohanh);
			td4.append(controlcustomcounter);
			td5.append("<input type='hidden' name='tuoitho_"+row.id+"' value='"+row.tuoitho+"' />" + number_format(row.tuoitho));
			thangbaohanh.kendoNumericTextBox({
				format: "n0",
				decimals: 0,
				min:0,
				spinners:false
			});
			counterbaohanh.kendoNumericTextBox({
				format: "n0",
				decimals: 0,
				min:0,
				spinners:false
			});
			
			var vendorchosen = $("<select name='linhkien_vendor_"+row.id+"'></select>");
			for(var i = 0;i < vendor.length ;i++)
			{
				var op = "<option value='"+vendor[i].id+"'>"+vendor[i].ma+" - "+vendor[i].ten+"</option>";
				vendorchosen.append(op);
			}
			td6.append(vendorchosen);
			td7.append("<i onclick='removeTrackerLinhkien(this)' style='line-height: 30px;cursor:pointer' class='icon-trash'></i>");
			$("#table_linhkien tbody").append(tr);
			vendorchosen.chosen();
		}
		
	}
}

function removeTrackerLinhkien(el)
{
	jQuery(el).closest("tr").remove();
}

function create_trackermachine()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=trackermachine&layout=create&tmpl=raw";
	var callback = function(){
		if($("#tab_create_trackermachine").length > 0)
		{
			initControl("#tab_create_trackermachine");
			initControlTrackerMachine();
			initCreatetrackermachine();
		}
	}
	obj.callback = callback;
	return obj;
}

function initControlTrackerMachine()
{
	$("#machine").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã máy</th><th class="k-header" >Tên máy</th></tr></thead></table></div></div>'
	});
	
	$("#machine").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}
		
		if($("#machine option:selected").length > 0)
		{
			var option = $("#machine option:selected");
			var sothang = $(option).attr("data-thang");
			var sodungluong = $(option).attr("data-dungluong");
			$("#form_create_trackermachine input[name='sothangbaohanh']").data("kendoNumericTextBox").value(sothang);
			$("#form_create_trackermachine input[name='sodungluongbaohanh']").data("kendoNumericTextBox").value(sodungluong);
		}	
	})

	$("#customer").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã khách hàng</th><th class="k-header" >Tên khách hàng</th></tr></thead></table></div></div>'
	});
	
	$("#customer").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}
	})
	
	$("#nhacungcap").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã nhà cung cấp</th><th class="k-header" >Tên nhà cung cấp</th></tr></thead></table></div></div>'
	});
	
	$("#nhacungcap").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}
	})

	$("#employee").chosentable({
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã nhân viên</th><th class="k-header" >Tên nhân viên</th></tr></thead></table></div></div>'
	});
	
	$("#hinhthuc").change(function(){
		var value = this.value;
		$(".hinhthuc_hidden").hide();
		$("." + value).show();
	})
	
	$("#hinhthuc").trigger("change");
	
	$(".linhkien_select").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã linh kiện</th><th class="k-header" >Tên linh kiện</th></tr></thead></table></div></div>'
	});
	
	$("#form_create_trackermachine input[name='sothangbaohanh']").kendoNumericTextBox({
		format: "n0",
		decimals: 0,
		min:0,
		spinners:false
	});
	
	$("#form_create_trackermachine input[name='sodungluongbaohanh']").kendoNumericTextBox({
		format: "n0",
		decimals: 0,
		min:0,
		spinners:false
	});
}

function edit_trackermachine()
{
	var localStorage = window.localStorage;	
	var item_arr = localStorage.getItem("edit_trackermachine");
	if(item_arr == null)
	{
		window.location.hash = $hash_url;
		return;
	}
	var item = JSON.parse(item_arr);
	var id = item.id;
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=trackermachine&layout=create&tmpl=raw&id="+id;
	var callback = function(){
		if($("#tab_create_trackermachine").length > 0)
		{
			initControl("#tab_create_trackermachine");
			initCreatetrackermachine();
		}
	}
	obj.callback = callback;
	return obj;
}

function initCreatetrackermachine()
{
	
	$("#form_create_trackermachine").validate({  
		errorPlacement: function(error, element) {
			if($(element).hasClass("error"))
			{
				hideLoader($('#form_create_trackermachine'));
			}
            error.remove();          
        },
        onkeyup:false,
        onfocusout:false,
        rules:{
            serial:{
                required:true,
                remote:{
        			url:"index.php?option=com_ketnoi&task=trackermachine.checkSerial&tmpl=raw",
        			data:{
        				serial:function(value){       	
        					return $("#form_create_trackermachine input[name='serial']").val();
        				}
        			},
        			beforeSend:function(){
        				showLoader($('#form_create_trackermachine'));
        			}
        		}
            },    
            socounter:{
            	required:true
            },    
            machine_id:{
            	required:true
            },    
            customer_id:{
            	required:true
            }
            
        },
        messages:{
        	serial:{
                required:'Vui lòng nhập serial', 
                remote:'Máy photocopy với số serial này đang hoạt động'
            },    
            socounter:{
            	required:"Vui lòng nhập số counter"
            },    
            machine_id:{
            	required:"Vui lòng chọn máy"
            },    
            customer_id:{
            	required:"Vui lòng chọn khách hàng"
            }
        }
    })
    
    $('#form_create_trackermachine').ajaxForm({
    	beforeSubmit:function(){   		
    		showLoader($('#form_create_trackermachine'));
    	},
    	success:function(response){
    		var obj = checkJSON(response);
    		if(obj.is_json)
    		{
    			if(!obj.result.success)
        		{
        			var error = obj.result.error;
        			alertify.alert(error);
        		}
    		}
    		else
    		{
    			//var container = getMainContent();
    			//container.html(response);
    			//disableHashchange();   			
    			window.location.hash = "trackermachine";
    			//initControl("#tab_create_trackermachine");
    			//initCreatetrackermachine();
    			//enableHashchange();
    				
    		}
    		hideLoader($('#form_create_trackermachine'));
    	}
    });
}

function edit_hash_trackermachine(id)
{
	item = {};
	item.id = id;
	setHash("edit_trackermachine",item);
	window.location.hash = "edit_trackermachine";
}

function deletetrackermachine(id)
{
	alertify.confirm("Bạn muốn xóa nhà cung cấp này?", function (e) {
	    if (e) {
	    	showLoader();
			var link = "index.php?option=com_ketnoi&view=trackermachine&task=trackermachine.delete&tmpl=raw&id="+id;
			$.get(link,"",function(response){
				var dataSource = $("#grid_trackermachine").data("kendoGrid").dataSource;
				dataSource.read();
				hideLoader();		
				alertify.set({ delay: 4000 });
				alertify.success("Xóa thành công");
			})
	    } 
	});
}



